// 这个是detail模块的vuex模块
import {reqGoodsDetailInfo} from '@/api'

// vuex当中的4个核心概念
const state = {
  // 存数据
  goodsDetailInfo : {}
}

const mutations = {
  // 直接修改数据
  RECEIVE_GOODSDETAILINFO(state, goodsDetailInfo){
    state.goodsDetailInfo = goodsDetailInfo
  }
}

const actions = {
  // 与组件当中用户对接
  async getGoodsDetailInfo({commit}, skuId){
    const result = await reqGoodsDetailInfo(skuId)
    if(result.code === 200){
      commit('RECEIVE_GOODSDETAILINFO', result.data)
    }
  }
  // 一般是异步发送请求
  // 提交mutations
}

const getters = {
  // 后面用来简化数据的操作
  categoryView(state){
    return state.goodsDetailInfo.categoryView || {}
  },
  skuInfo(state){
    return state.goodsDetailInfo.skuInfo || {}
  },
  spuSaleAttrList(state){
    return state.goodsDetailInfo.spuSaleAttrList || []
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}